<?php
namespace frontend\controllers;

use common\actions\SmsAction;
use common\helpers\AjaxHelper;
use common\models\Article;
use common\models\Check;
use common\models\SmsForm;
use common\models\UserCheck;
use frontend\models\SignupForm;
use Yii;
use yii\helpers\Url;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

/**
 * Site controller
 */
class SiteController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'sms' => [
                'class' => SmsAction::className(),
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        return $this->render('index');
    }
    
    /**
     * 了解会员权益
     * @return string
     */
    public function actionLiaojie()
    {
        return $this->render('liaojie');
    }
    
    public function actionBanli()
    {
        $is_show = Yii::$app->request->getQueryParam('show');
        
        if( $is_show ){
            return $this->render('banli');
        }else{
            $article = Article::find()->where(['classify' => 3])->orderBy(['id' => SORT_DESC])->one();
            return $this->render('tip', ['model' => $article, 'url' => Url::toRoute(['site/banli', 'show' => 1])]);
        }
    }
    
    /**
     * 会员须知
     */
    public function actionXuzhi()
    {
        $list = Article::find()->where(['classify'=>1])->all();
        return $this->render('xuzhi', ['list' => $list]);
    }
    
    /**
     * 文章详情页
     */
    public function actionArticle($id)
    {
        $model = Article::findOne($id);
        return $this->render('article', ['model' => $model]);
    }
    
    
    /**
     * 用户注册
     */
    public function actionSignup()
    {
        // 是会员的话，就跳到个人中心
        $user = Yii::$app->user->identity;
        if( $user && $user->isVip() ){
            Yii::$app->response->redirect(Url::toRoute(['member/index']))->send();
        }
        
        if( $user && !$user->isVip() ){
            if( $user->isUserInfo() ){
                // 需要完善信息
                Yii::$app->response->redirect(Url::toRoute(['member/info']))->send();
            }else{
                $check = Check::find()->where(['user_id' => Yii::$app->user->id])->one();
                // 判断是否需要申请
                if( UserCheck::findOne(['id' => 1])->shenqing == 1 && $check->times != 3 ){
                    // 填写申请
                    Yii::$app->response->redirect(Url::toRoute(['member/shenqing', 'type'=>1]));
                }else{
                    // 购买会员
                    Yii::$app->response->redirect(Url::toRoute(['member/vip-buy']));
                }
            }
        }
        
        
        $this->layout = false;
       
        return $this->render('signup');
    }
    
    public function actionDoSignup()
    {
        // 查理处理
        $model = new SignupForm();
        $model->attributes = Yii::$app->request->getBodyParams();
        
        $response = new AjaxHelper;
        if( $user = $model->signup() ){
            if (Yii::$app->getUser()->login($user, 15*3600*24)) {
                $response->data = ['url' => Yii::$app->getHomeUrl()];
            }
        }else{
            $response->msg = $model->getOneErrors();
        }
        
        return $response;
    }
    

}
